<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('captcha');
    }

    public function index() {
        //redireccion segura
        if ($this->session->userdata('access')) {
            redirect('xpanel');
        }
        $vals = array(
            'img_path' => './assets/captcha/',
            'img_url' => base_url() . "assets/captcha/",
            'font_path' => './assets/captcha/fonts/OpenSans-Regular.ttf',
            'img_width' => '250',
            'img_height' => 30,
            'expiration' => 7200
        );

        $cap = create_captcha($vals);
        // se agrega el captcha a la base de datos
        $this->usuario_model->insertCaptcha($cap['time'], $this->input->ip_address(), $cap['word']);
        $data['cap'] = $cap;
        $this->load->view('login/login_view', $data);
    }

    public function x() {

        # SET VALIDATION RULES
        $this->form_validation->set_rules('captcha', 'Captcha', 'required|callback_captcha_check');
        $this->form_validation->set_rules('user', 'username', 'required|trim|xss_clean|min_length[6]|max_length[15]');
        $this->form_validation->set_rules('pass', 'password', 'required|trim|xss_clean|min_length[6]|max_length[20]');

        # has the form been submitted and with valid form info (not empty values)
        if (!$this->form_validation->run()) {
            redirect('home');
        }

        # autenticacion segura
        $usuario = $this->security->xss_clean($this->input->post('user'));
        $pass = $this->security->xss_clean($this->input->post('pass'));

        if ($this->verifica($usuario, $pass)) {//diferentes tipos de empleados
            $this->usuario_model->deleteCaptcha($this->input->ip_address());
            $admin = $this->session->userdata('admin');
            $l_ip = $this->input->ip_address();
            $l_nombre = $admin['e_nombre'];
            $this->usuario_model->regAcceso($l_ip,$l_nombre);
            switch ($admin['te_tipo_empleado_id']) {
                case 1:#usuario tipo 1
                    redirect('xpanel');
                    break;
            }
        } else {
            redirect('home');
        }
    }
    
    function captcha_check() {
        $expiration = time() - 7200; // Limite de dos horas
        $binds = array($this->input->post('captcha'), $this->input->ip_address(), $expiration);
        return $this->usuario_model->captchaExist($binds);
    }

    private function verifica($usuario, $pass) {
        $result = $this->usuario_model->verificar_datos($usuario, $pass);
        #existe usuario
        if (is_object($result)) {
            $admin['te_tipo_empleado_id'] = $result->te_tipo_empleado_id;
            $admin['te_nombre'] = $result->te_nombre;
            $admin['e_empleado_id'] = $result->e_empleado_id;
            $admin['e_nombre'] = $result->e_nombre;
            $admin['e_usuario'] = $result->e_usuario;
            $admin['e_email'] = $result->e_email;
            $admin['d_division_id'] = $result->d_division_id;
            $admin['d_nombre'] = $result->d_nombre;
            $admin['e_empresa_id'] = $result->e_empresa_id;
            $admin['em_nombre'] = $result->em_nombre;
            $admin['e_name_imagen'] = $result->e_name_imagen;
            $this->session->set_userdata('admin', $admin);
            return true;
        }
        # datos no existentes
        return false;
    }

}
